package com.web.ShoppingDemo.util;

import java.util.ArrayList;
import java.util.List;
import java.util.TimerTask;

import javax.annotation.Resource;
import javax.servlet.ServletContext;

import org.springframework.stereotype.Component;

import com.web.ShoppingDemo.model.Category;
import com.web.ShoppingDemo.model.Product;
import com.web.ShoppingDemo.service.CategoryService;
import com.web.ShoppingDemo.service.ProductService;
/**
 * 设置任务:run方法用来加载首页商品信息
 * @author THT
 *
 */
@Component("productTimerTask")
public class ProductTimerTask extends TimerTask{
	@Resource
	private ProductService productService = null;
	@Resource
	private CategoryService categoryService = null;
	
	private ServletContext application = null;
	
	public void setApplication(ServletContext application) {
		this.application = application;
	}
	
	@Override
	public void run() {
		System.out.println("-----run-----");
		
		List<List<Product>> bigList = new ArrayList<List<Product>>();
		//查询出热点类别
		for(Category category:categoryService.queryByHots(true)){
			//根据热点类别的id获取推荐商品的信息
			bigList.add(productService.queryByCid(category.getId()));
		}
		//查询的bigList交给application内置对象
		application.setAttribute("bigList", bigList);
	}
	
	/* 使用Timer安排守护线程,设置true则设置成为守护线程
	public static void main(String[] args) {
		new Timer(false).schedule(new TimerTask(){
			@Override
			public void run() {
				System.out.println("-----run-----");
			}
		}, 0, 2000);
	}*/

}
